﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace IndexingHandler
{
    public class LogHandler
    {
        public string fileName;

        // Функция получения пути для лог файла по умолчанию
        private string getDefaultLogName()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path) + "\\log.log";
        }

        public LogHandler()
        {
            fileName = getDefaultLogName();
        }

        public LogHandler(string FileName)
        {
            fileName = FileName;
        }

        public void WriteLogStr(string message)
        {
            using (StreamWriter streamWriter = new StreamWriter(new FileStream(fileName, FileMode.Append)))
            {
                streamWriter.WriteLine("PID [" + Process.GetCurrentProcess().Id  + "] {0} {1}", DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff"), message);
            }
        }

        public void WriteMessage(string message)
        {
            using (StreamWriter streamWriter = new StreamWriter(new FileStream(fileName, FileMode.Append)))
            {
                streamWriter.WriteLine("PID [" + Process.GetCurrentProcess().Id  + "] " + message);
            }
        }
    }
}
